<?php

namespace Seller\Controller;

class DispatchingController extends BaseController {

    private $_model = nill;

    protected function _initialize() {
        parent::_initialize();
        $this->_model = D('dispatching');
    }
    
    public function index(){
        $data['d.customer_id'] = $this->__seller_info['id'];
        
         $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $count = M('dispatching')->alias('d')->where($where)->count();
        //分页
        $pagesize = 20; #每页数量
        $offset = $pagesize * ($p - 1); //计算记录偏移量
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $limit = $offset . ',' . $pagesize;
        $list = M('dispatching')->alias('d')->where($data)->limit($offset . ',' . $pagesize)->select();
        $this->assign('lists',$list);
        $this->assign('page',$page);
        $this->display();
    }
       /**
     * 模板增加
     */
    public function  add(){
        if(IS_POST){
            if(empty($this->__post['type'])){
                $this->error('请选择类型！');
                exit;
            }
           if(count($this->__post['city_id']) <= 0){
                $this->error('至少绑定一个城市！');
                exit;
            }
            if(count($this->__post['warehouse_id']) <= 0){
                $this->error('至少绑定一个仓库！');
                exit;
            }
            if(M('dispatching')->where(array('customer_id'=>$this->__seller_info['id'],'name'=>$this->__post['name']))->find()){
                 $this->error('你已经添加该配送模板！');
                exit;
            }
            $data = $this->__post;
            $data['warehouse_id'] = implode(',', $data['warehouse_id']);
            if(strlen($data['warehouse_id']>0)){
                $data['warehouse_id'] = ','.$data['warehouse_id'].',';
            }
            $data['city_id'] = implode(',', $data['city_id']);
            if(strlen($data['city_id']>0)){
                $data['city_id'] = ','.$data['city_id'].',';
            }
            $data['customer_id'] = $this->__seller_info['id'];
            $data['create_time'] = time();
            if(M('dispatching')->add($data)){
                     $this->success('创建成功',U('index'));
            }else{
                  $this->error('创建失败！');
            }
        }else{
			$this->assign('warehouse',M('customer_warehouse')->where(array('customer_id' => $this->__seller_info['id']))->select());
            $this->assign('address',M('area')->where(array('parentid' => 0))->select());
            $this->display('form');
        }
        
    }
       /**
     * 模板修改
     */
    public function edit() {
        $id = $this->__get['id'];
        if(IS_POST){
            if(count($this->__post['city_id']) <= 0){
                $this->error('至少绑定一个城市！');
                exit;
            }
            if(count($this->__post['warehouse_id']) <= 0){
                $this->error('至少绑定一个仓库！');
                exit;
            }
            $data = $this->__post;
           
            $data['warehouse_id'] = implode(',', $data['warehouse_id']);
            if(strlen($data['warehouse_id']>0)){
                $data['warehouse_id'] = ','.$data['warehouse_id'].',';
            }
            $data['city_id'] = implode(',', $data['city_id']);
            if(strlen($data['city_id']>0)){
                $data['city_id'] = ','.$data['city_id'].',';
            }
            if(M('dispatching')->where(array('id'=>$id,'customer_id'=>$this->__seller_info['id']))->save($data)){
                   $this->success('修改成功',U('index'));
            }else{
                  $this->error('修改失败！');
            }
        }else{
         $info = M('dispatching')->where(array('id'=>$id))->find();
         $this->assign('warehouse',M('customer_warehouse')->where(array('customer_id' => $this->__seller_info['id']))->select());
        if(strlen(substr($info['goods'],1,strlen($info['goods'])-2))>0){
             $goods = explode(',',substr($info['goods'],1,strlen($info['goods'])-2));
        }else{
            $goods = array();
        }
     
         $this->assign('address',M('area')->where(array('parentid' => 0))->select());
        if(strlen(substr($info['city_id'],1,strlen($info['city_id'])-2))>0){
            $info['city_id'] = explode(',',substr($info['city_id'],1,strlen($info['city_id'])-2));
        }else{
           $info['city_id'] = array();
        }
        if(strlen(substr($info['warehouse_id'],1,strlen($info['warehouse_id'])-2))>0){
            $info['warehouse_id'] = explode(',',substr($info['warehouse_id'],1,strlen($info['warehouse_id'])-2));
        }else{
           $info['warehouse_id'] = array();
        }
         $this->assign('goods',$goods);
         $this->assign('info',$info);
         $this->display('form');
        }
    }
    
    
     /**
     * 模板删除
     */
    public function del() {
        if (!isset($this->__post['id']) || intval($this->__post['id']) <= 0)
            exit('信息不存在');
        if( $this->_model->where(array('id' => $this->__post['id'], 'customer_id' => $this->__seller_info['id']))->delete() ) {
            $this->success('配送模板删除成功',U('index'));
        } else {
            $this->error('删除失败');
        }
    }
    
    
    public function search(){
        $keyname = I('get.keyname');
        $data['errno'] = 0;
        $data['msg'] = '失败';
         if ($keyname && !empty($keyname)) {
            $where['name'] = array('like', "%{$keyname}%");
        }
        $where['customer_id'] = $this->__seller_info['id'];
        $goodList = M('goods')->where($where)->select();
        if($goodList){
            $data['errno'] =1;
            $data['data'] = $goodList;
             $data['msg'] = '成功';
        }
       $this->ajaxReturn($data);
       die;
        
    }
}

